var orderFormWindow = null;
var orderItemsDataStore = null;
var orderColumnModel = null;
var orderEditorGrid = null;

function createOrderForm()
{	
	orderItemsDataStore = new Ext.data.Store({
		id: 'orderItemsDataStore',
		reader: new Ext.data.JsonReader({
				root: 'results',
				totalProperty: 'total',
				id: 'id'
			},[ 
				{name: 'id', type: 'int', mapping: 'id'},
				{name: 'article', type: 'string', mapping: 'article'},
				{name: 'description', type: 'string', mapping: 'description'},
				//{name: 'groupID', type: 'int', mapping: 'groupID'},
				//{name: 'suplyPrice', type: 'float', mapping: 'suplyPrice'},
				//{name: 'suplyDiscount', type: 'int', mapping: 'suplyDiscount'},
				{name: 'suplyPriceRUB', type: 'float', mapping: 'suplyPriceRUB'},
				//{name: 'discount', type: 'int', mapping: 'discount'},
				{name: 'price', type: 'float', mapping: 'price'},
				{name: 'secCount', type: 'int', mapping: 'secCount'},
				{name: 'count', type: 'int', mapping: 'count'},
				{name: 'sum', type: 'float', mapping: 'sum'},
				{name: 'oSum', type: 'float', mapping: 'oSum'},
				
				/*{name: 'currency', type: 'string', mapping: 'currency'},
				{name: 't50', type: 'float', mapping: 't50'},
				{name: 't60', type: 'float', mapping: 't60'},
				{name: 't70', type: 'float', mapping: 't70'},
				{name: 'P', type: 'int', mapping: 'P'},
				{name: 'I', type: 'int', mapping: 'I'},
				{name: 'width', type: 'int', mapping: 'width'},
				{name: 'height', type: 'int', mapping: 'height'},
				{name: 'deep', type: 'int', mapping: 'deep'},
				{name: 'length', type: 'int', mapping: 'length'},
				{name: 'partly', type: 'int', mapping: 'partly'},
				{name: 'intercenterlen', type: 'int', mapping: 'intercenterlen'},
				{name: 'weight', type: 'float', mapping: 'weight'},
				{name: 'companyID', type: 'int', mapping: 'companyID'},
				{name: 'companyName', type: 'string', mapping: 'companyName'},
				{name: 'groupName', type: 'string', mapping: 'groupName'}*/
		]),
		sortInfo:{field: 'id', direction: "ASC"}
    });
	orderColumnModel = new Ext.grid.ColumnModel(
    [{
        header: 'Артикул',
        dataIndex: 'article',
        width: 120//,
        //editor: new Ext.form.TextField({allowBlank: false,maxLength: 255})
      },{
		id:'secCount',
		header: 'Кол-во сек.',
        dataIndex: 'secCount',
        width: 70,
		hidden: true,
        editor: new Ext.form.NumberField({allowBlank: true,allowDecimals: false,allowNegative: false,blankText: '1',maxLength: 11})
	  },{
        header: 'Описание',
        dataIndex: 'description',
        width: 180//,
        //editor: new Ext.form.TextField({allowBlank: true}),
      },{
        header: 'Опт. руб.',
        readOnly: true,
        dataIndex: 'suplyPriceRUB',
        width: 55,
		css: 'font-weight: bold;'
      },{
        header: 'Цена',
        dataIndex: 'price',
        width: 50,
		css: 'font-weight: bold;',
        editor: new Ext.form.NumberField({allowBlank: false,allowDecimals: true,allowNegative: false,blankText: '0',maxLength: 11})
      },{
		header: 'Кол-во',
        dataIndex: 'count',
        width: 50,
        editor: new Ext.form.NumberField({allowBlank: false,allowDecimals: false,allowNegative: false,blankText: '0',maxLength: 11})
	  },{
		header: 'Сумма',
        dataIndex: 'sum',
        width: 50
	  }
	  
	  ]
    );
	orderEditorGrid = new Ext.grid.EditorGridPanel({
		id: 'orderEditorGrid',
		store: orderItemsDataStore,
		cm: orderColumnModel,
		enableColLock:false,
		clicksToEdit:2,
		selModel: new Ext.grid.RowSelectionModel({singleSelect:false}),
		bbar:new Ext.Toolbar({items:
			[
				{xtype: 'tbtext', text: 'Размер скидки, %:'},
				{xtype: 'textfield', name: 'discount4Customer', emptyText: ''},'->',
				{xtype: 'tbtext', text: 'Сумма опт.:'},{xtype: 'tbtext', text: ''},
				' ','-',' ',{xtype: 'tbtext', text: 'Сумма розн.:'},{xtype: 'tbtext', text: ''}
			]})
	});
	orderEditorGrid.on('afteredit', recalcItem);
	orderFormWindow = new Ext.Window({id: 'orderFormWindow',title: 'Заказ',closable:true,width: 710,height: 300,plain:true,layout: 'fit',
      items: orderEditorGrid
    });
}
function addToOrder()
{
	if (orderFormWindow!=null)
	{
		var row = FullPriceDataStore.getAt(FullPriceSelectedRow);
		var new_row = {article: row.data.article, 
			description: row.data.description,
			suplyPriceRUB: row.data.suplyPriceRUB,
			price: row.data.price,
			count: 1,
			secCount: 1,
			sum: row.data.price,
			oSum: row.data.suplyPriceRUB
		};
		if (row.data.groupID==1) orderColumnModel.setHidden(1,false);
		orderItemsDataStore.loadData( {total: 1, results:[new_row]} ,true);
		refreshSum();
	}
}
function refreshSum()
{
	var rSum = 0;
	var oSum = 0;
	orderItemsDataStore.each( function(rec) { rSum += rec.data.sum; oSum += rec.data.oSum});
	var bar = orderEditorGrid.getBottomToolbar();
	bar.items.get(4).setText(oSum);
	bar.items.get(9).setText(rSum);
}
function showOrderForm()
{
	if (orderFormWindow==null) createOrderForm();
	orderFormWindow.show();
}
function recalcItem(oGrid_event)
{
	oGrid_event.record.data.sum = oGrid_event.record.data.secCount * oGrid_event.record.data.count * oGrid_event.record.data.price;
	oGrid_event.record.data.oSum = oGrid_event.record.data.secCount * oGrid_event.record.data.count * oGrid_event.record.data.suplyPriceRUB;
	orderItemsDataStore.commitChanges();
	refreshSum();
}